package com.longge;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 xinglongge
 * @创建时间 2021/9/23
 * @描述
 */
public class SpiralOrder {

    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        while (matrix.length>=1){
            for (int num : matrix[0]) {
                res.add(num);
            }
            matrix=reversalArr(matrix);
        }
        return res;
    }


    private int[][] reversalArr(int[][] matrix) {
        int m = matrix[0].length;
        int n = matrix.length - 1;
        int[][] reArr = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                reArr[i][j]=matrix[j+1][m-i-1];
            }
        }
        return reArr;
    }

    public static void main(String[] args) {
        List<Integer> integers = new SpiralOrder().spiralOrder(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
        System.out.println(integers);
    }
}
